
#include <document_manager.h>
#include "document_header.h"
#include "uimanager.h"




static glong string_get_length (const char* self);
enum  {
	DOCUMENT_MANAGER_DUMMY_PROPERTY,
	DOCUMENT_MANAGER_CURRENT_DOC
};
static DocumentManager* document_manager_instance = NULL;
static DocumentManager* document_manager_construct (GType object_type);
static DocumentManager* document_manager_new (void);
static void _document_manager_on_page_added_gtk_notebook_page_added (DocumentManager* _sender, GtkWidget* p0, guint p1, gpointer self);
static void _document_manager_on_page_removed_gtk_notebook_page_removed (DocumentManager* _sender, GtkWidget* p0, guint p1, gpointer self);
static GObject * document_manager_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static gpointer document_manager_parent_class = NULL;
static void document_manager_finalize (GObject* obj);



static glong string_get_length (const char* self) {
	g_return_val_if_fail (self != NULL, 0L);
	return g_utf8_strlen (self, -1);
}


static DocumentManager* document_manager_construct (GType object_type) {
	DocumentManager * self;
	self = g_object_newv (object_type, 0, NULL);
	return self;
}


static DocumentManager* document_manager_new (void) {
	return document_manager_construct (TYPE_DOCUMENT_MANAGER);
}


/*Tworzy dokument */
gboolean document_manager_create (DocumentManager* self, const char* filename) {
	gboolean _tmp0;
	gboolean _tmp1;
	gboolean _tmp2;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0 = FALSE;
	_tmp1 = FALSE;
	_tmp2 = FALSE;
	if (filename != NULL) {
		_tmp2 = document_manager_get_current_doc (self) != NULL;
	} else {
		_tmp2 = FALSE;
	}
	if (_tmp2) {
		_tmp1 = document_get_state (document_manager_get_current_doc (self)) == DOCUMENT_DOCUMENT_STATE_NO_MODIFIED;
	} else {
		_tmp1 = FALSE;
	}
	if (_tmp1) {
		char* _tmp3;
		_tmp3 = NULL;
		_tmp0 = string_get_length (_tmp3 = document_get_content (document_manager_get_current_doc (self))) == 0;
		_tmp3 = (g_free (_tmp3), NULL);
	} else {
		_tmp0 = FALSE;
	}
	if (_tmp0) {
		char* _tmp4;
		document_set_contents_from_file (document_manager_get_current_doc (self), filename);
		_tmp4 = NULL;
		document_header_set_text ((DOCUMENT_HEADER (gtk_notebook_get_tab_label ((GtkNotebook*) self, (GtkWidget*) document_manager_get_current_doc (self)))), _tmp4 = g_path_get_basename (filename));
		_tmp4 = (g_free (_tmp4), NULL);
	} else {
		Document* doc;
		DocumentHeader* _tmp5;
		doc = g_object_ref_sink (document_new (filename));
		_tmp5 = NULL;
		gtk_notebook_set_current_page ((GtkNotebook*) self, gtk_notebook_append_page ((GtkNotebook*) self, (GtkWidget*) doc, (GtkWidget*) (_tmp5 = g_object_ref_sink (document_header_new (filename, &doc)))));
		(_tmp5 == NULL) ? NULL : (_tmp5 = (g_object_unref (_tmp5), NULL));
		gtk_notebook_set_tab_reorderable ((GtkNotebook*) self, (GtkWidget*) doc, TRUE);
		document_grab_focus (doc);
		(doc == NULL) ? NULL : (doc = (g_object_unref (doc), NULL));
	}
	return TRUE;
}


/*Otwiera plik za pomocą okna...*/
void document_manager_open (DocumentManager* self) {
	GtkFileChooserDialog* dialog;
	GtkFileFilter* filter;
	g_return_if_fail (self != NULL);
	dialog = g_object_ref_sink ((GtkFileChooserDialog*) gtk_file_chooser_dialog_new ("Otwórz...", NULL, GTK_FILE_CHOOSER_ACTION_OPEN, GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL));
	gtk_file_chooser_set_select_multiple ((GtkFileChooser*) dialog, TRUE);
	filter = g_object_ref_sink (gtk_file_filter_new ());
	gtk_file_filter_add_mime_type (filter, "text/*");
	gtk_file_chooser_set_filter ((GtkFileChooser*) dialog, filter);
	if (gtk_dialog_run ((GtkDialog*) dialog) == GTK_RESPONSE_ACCEPT) {
		GSList* list;
		list = gtk_file_chooser_get_filenames ((GtkFileChooser*) dialog);
		{
			GSList* file_collection;
			GSList* file_it;
			file_collection = list;
			for (file_it = file_collection; file_it != NULL; file_it = file_it->next) {
				const char* _tmp0;
				char* file;
				_tmp0 = NULL;
				file = (_tmp0 = (const char*) file_it->data, (_tmp0 == NULL) ? NULL : g_strdup (_tmp0));
				{
					document_manager_create (self, file);
					file = (g_free (file), NULL);
				}
			}
		}
	}
	gtk_object_destroy ((GtkObject*) dialog);
	(dialog == NULL) ? NULL : (dialog = (g_object_unref (dialog), NULL));
	(filter == NULL) ? NULL : (filter = (g_object_unref (filter), NULL));
}


void document_manager_save_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_save (document_manager_get_current_doc (self));
}


void document_manager_save_as_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_save_as (document_manager_get_current_doc (self));
}


void document_manager_save_all (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	{
		gint i;
		i = gtk_notebook_get_n_pages ((GtkNotebook*) self) - 1;
		for (; i >= 0; (i = i - 1)) {
			Document* _tmp0;
			Document* doc;
			_tmp0 = NULL;
			doc = (_tmp0 = DOCUMENT (gtk_notebook_get_nth_page ((GtkNotebook*) self, i)), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0));
			document_save (doc);
			(doc == NULL) ? NULL : (doc = (g_object_unref (doc), NULL));
		}
	}
}


/*Zamyka aktualny dokumnt*/
void document_manager_close_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	if (document_close (document_manager_get_current_doc (self))) {
		gtk_notebook_remove_page ((GtkNotebook*) self, gtk_notebook_get_current_page ((GtkNotebook*) self));
	}
}


/*Zamyka wszytskie otwarte dokumenty
Zwraca false gdy anulowano zamykanie, true gdy wszytsko sie powiodło*/
gboolean document_manager_close_all (DocumentManager* self) {
	g_return_val_if_fail (self != NULL, FALSE);
	{
		gint i;
		i = gtk_notebook_get_n_pages ((GtkNotebook*) self) - 1;
		for (; i >= 0; (i = i - 1)) {
			Document* _tmp0;
			Document* doc;
			gtk_notebook_set_current_page ((GtkNotebook*) self, i);
			_tmp0 = NULL;
			doc = (_tmp0 = DOCUMENT (gtk_notebook_get_nth_page ((GtkNotebook*) self, i)), (_tmp0 == NULL) ? NULL : g_object_ref (_tmp0));
			if (document_close (doc)) {
				gtk_notebook_remove_page ((GtkNotebook*) self, i);
				gtk_object_destroy ((GtkObject*) doc);
			} else {
				gboolean _tmp1;
				return (_tmp1 = FALSE, (doc == NULL) ? NULL : (doc = (g_object_unref (doc), NULL)), _tmp1);
			}
			(doc == NULL) ? NULL : (doc = (g_object_unref (doc), NULL));
		}
	}
	return TRUE;
}


void document_manager_undo_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_undo (document_manager_get_current_doc (self));
}


void document_manager_redo_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_redo (document_manager_get_current_doc (self));
}


void document_manager_cut_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_cut (document_manager_get_current_doc (self));
}


void document_manager_copy_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_copy (document_manager_get_current_doc (self));
}


void document_manager_paste_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_paste (document_manager_get_current_doc (self));
}


void document_manager_delete_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_delete_selection (document_manager_get_current_doc (self));
}


void document_manager_select_all_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_select_all (document_manager_get_current_doc (self));
}


void document_manager_search_in_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_show_search_panel (document_manager_get_current_doc (self));
}


void document_manager_replace_in_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_show_replace_panel (document_manager_get_current_doc (self));
}


void document_manager_goto_line_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_show_goto_line_panel (document_manager_get_current_doc (self));
}


void document_manager_goto_next_found_match_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_goto_next_found_match (document_manager_get_current_doc (self));
}


void document_manager_goto_prev_found_match_current (DocumentManager* self) {
	g_return_if_fail (self != NULL);
	document_goto_prev_found_match (document_manager_get_current_doc (self));
}


DocumentManager* document_manager_get_instance (void) {
	if (document_manager_instance == NULL) {
		DocumentManager* _tmp0;
		_tmp0 = NULL;
		document_manager_instance = (_tmp0 = g_object_ref_sink (document_manager_new ()), (document_manager_instance == NULL) ? NULL : (document_manager_instance = (g_object_unref (document_manager_instance), NULL)), _tmp0);
	}
	return document_manager_instance;
}


void document_manager_on_page_added (DocumentManager* self, DocumentManager* document_manager, GtkWidget* p0, guint p1) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (document_manager != NULL);
	g_return_if_fail (p0 != NULL);
	if (gtk_notebook_get_n_pages ((GtkNotebook*) self) == 1) {
		UIManager* _tmp0;
		_tmp0 = NULL;
		ui_manager_set_sensitive_doc_actions (_tmp0 = ui_manager_get_instance (), TRUE);
		(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
	}
}


void document_manager_on_page_removed (DocumentManager* self, DocumentManager* document_manager, GtkWidget* p0, guint p1) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (document_manager != NULL);
	g_return_if_fail (p0 != NULL);
	if (gtk_notebook_get_n_pages ((GtkNotebook*) self) == 0) {
		UIManager* _tmp0;
		_tmp0 = NULL;
		ui_manager_set_sensitive_doc_actions (_tmp0 = ui_manager_get_instance (), FALSE);
		(_tmp0 == NULL) ? NULL : (_tmp0 = (g_object_unref (_tmp0), NULL));
	}
}


Document* document_manager_get_current_doc (DocumentManager* self) {
	g_return_val_if_fail (self != NULL, NULL);
	return DOCUMENT (gtk_notebook_get_nth_page ((GtkNotebook*) self, gtk_notebook_get_current_page ((GtkNotebook*) self)));
}


static void _document_manager_on_page_added_gtk_notebook_page_added (DocumentManager* _sender, GtkWidget* p0, guint p1, gpointer self) {
	document_manager_on_page_added (self, _sender, p0, p1);
}


static void _document_manager_on_page_removed_gtk_notebook_page_removed (DocumentManager* _sender, GtkWidget* p0, guint p1, gpointer self) {
	document_manager_on_page_removed (self, _sender, p0, p1);
}


static GObject * document_manager_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	DocumentManagerClass * klass;
	GObjectClass * parent_class;
	DocumentManager * self;
	klass = DOCUMENT_MANAGER_CLASS (g_type_class_peek (TYPE_DOCUMENT_MANAGER));
	parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DOCUMENT_MANAGER (obj);
	{
		g_signal_connect_object ((GtkNotebook*) self, "page-added", (GCallback) _document_manager_on_page_added_gtk_notebook_page_added, self, 0);
		g_signal_connect_object ((GtkNotebook*) self, "page-removed", (GCallback) _document_manager_on_page_removed_gtk_notebook_page_removed, self, 0);
	}
	return obj;
}


static void document_manager_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DocumentManager * self;
	gpointer boxed;
	self = DOCUMENT_MANAGER (object);
	switch (property_id) {
		case DOCUMENT_MANAGER_CURRENT_DOC:
		g_value_set_object (value, document_manager_get_current_doc (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void document_manager_class_init (DocumentManagerClass * klass) {
	document_manager_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->get_property = document_manager_get_property;
	G_OBJECT_CLASS (klass)->constructor = document_manager_constructor;
	G_OBJECT_CLASS (klass)->finalize = document_manager_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DOCUMENT_MANAGER_CURRENT_DOC, g_param_spec_object ("current-doc", "current-doc", "current-doc", TYPE_DOCUMENT, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
}


static void document_manager_instance_init (DocumentManager * self) {
}


static void document_manager_finalize (GObject* obj) {
	DocumentManager * self;
	self = DOCUMENT_MANAGER (obj);
	G_OBJECT_CLASS (document_manager_parent_class)->finalize (obj);
}


GType document_manager_get_type (void) {
	static GType document_manager_type_id = 0;
	if (document_manager_type_id == 0) {
		static const GTypeInfo g_define_type_info = { sizeof (DocumentManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) document_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DocumentManager), 0, (GInstanceInitFunc) document_manager_instance_init, NULL };
		document_manager_type_id = g_type_register_static (GTK_TYPE_NOTEBOOK, "DocumentManager", &g_define_type_info, 0);
	}
	return document_manager_type_id;
}




